<?php
defined('APP_PATH') or die ('Unauthorized access'); 

/**
 * index
 * @author Chris Zhou
 *
 */
class ListController extends Controller
{
	private $_blog = null;
	private $_pager = null;
	public function __construct()
	{
		$this->_blog = Load::Model('blog');
		$this->_pager = Load::Ext('page');
	}
	
	public function index()
	{
		$this->view->title = '風之紫色\'blog';
		
		$cid = intval($this->request('id'));
		
		$tags = $this->request('tags');
		
		$page = !$this->request('p') ? 1 : intval($this->request('p'));				 
		$page = $page == 0 ? 1 : $page;
		
		$pagesize = 10;
		$total = $this->_blog->getBlogCount($cid,$tags);
		
		$this->_pager -> config = array(
				'page' => $page,
				'pagesize' => $pagesize,
				'total' => $total,
				'size' => 10,
				'url' => $tags ? "/?c=list&tags={$tags}" : "/?c=list&id=$cid&p="
			);
		$pagebar = $this->_pager->layOut();
		
		if($tags){
			$tags = addslashes(strip_tags($tags));
			$bloglist = $this->_blog->getBlogAsTags($tags,$page,$pagesize);
		}else{
			$bloglist = $this->_blog->getBlogList($cid,$page,$pagesize);
		}
		
		if(!empty($bloglist) && count($bloglist) > 0){
			foreach($bloglist as $k => $list){
				$tags = explode(' ',$list['tags']);
				$bloglist[$k]['tags'] = $tags;
			}
		}
		$this->view->bloglist = $bloglist;
		$this->view->pagebar = $pagebar;
	}
}